package com.nq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nq.common.ServerResponse;
import com.nq.dao.stock.StCommissionLevelMapper;
import com.nq.service.StCommissionLevelService;
import com.nq.stock.entity.StCommissionLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class StCommissionLevelServiceImpl implements StCommissionLevelService {


    @Autowired
    private StCommissionLevelMapper commissionLevelMapper;

    @Override
    public List<StCommissionLevel> listSetting() {
        List<StCommissionLevel> commissionLevels = commissionLevelMapper.selectList(new QueryWrapper<>());
        return commissionLevels;
    }

    @Override
    public ServerResponse updateOrAddSetting(Integer id, Integer level, String proportion, HttpServletRequest request) {
        // 更新或新增
        StCommissionLevel commissionLevel = new StCommissionLevel();
        commissionLevel.setLevel(level);
        commissionLevel.setProportion(new BigDecimal(proportion));
        if (null == id) {
            commissionLevel.setCreateTime(new Date());
            commissionLevelMapper.insert(commissionLevel);
        }else {
            commissionLevel.setId(id);
            commissionLevelMapper.updateById(commissionLevel);
        }
        return ServerResponse.createBySuccess("编辑成功");
    }

    @Override
    public ServerResponse delSetting(Integer id, HttpServletRequest request) {
        commissionLevelMapper.deleteById(id);
        return ServerResponse.createBySuccess("删除成功");
    }
}
